﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;

using Analysis.Entities;
using Analysis.DAL;
using Analysis.DAL.Repositories;

namespace Analysis.Code
{
    public class BasePage : Page
    {
        public string JsCodeToRunOnReady { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            JsCodeToRunOnReady = "";
        }

        public bool IsAuthentificated
        {
            get { return System.Web.HttpContext.Current.User.Identity.IsAuthenticated; }
        }

        public string BasePath
        {
            get { return ResolveClientUrl("~"); }
        }

        public bool IsAdmin
        {
            get
            {
                if (IsAuthentificated)
                    return Roles.IsUserInRole("admin");

                return false;
            }

        }

        public User CurrentUser
        {
            get
            {
                using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
                {
                    return repo.Find(Membership.GetUser().UserName);
                }
            }
        }

        public Guid CurrentUserId
        {
            get
            {
                return (Guid)Membership.GetUser().ProviderUserKey;
            }
        }

        public Depart CurrentUserDepart
        {
            get
            {
                using (var repo = RepositoryFactory.ResolveRepository<SetupRepository>())
                {
                    return repo.FindDepartById(CurrentUser.DepartId);
                }
            }
        }

        protected string GetIPAddress()
        {
            return Request.UserHostAddress;
        }

        protected void AlertAndClear(string message)
        {
            Response.Clear();
            Response.Write(string.Format("<script type=\"text/javascript\">alert('{0}');</script>", message));
            Response.End();
        }

        public bool IsIE()
        {
            return ((Request.Browser.Browser.ToLower() == "ie") || (Request.Browser.Browser.ToLower() == "mozilla"));
        }

        protected bool AccessConfirm(string pageName)
        {
            if (IsAdmin)
            {
                return true;
            }
            else
            {
                bool result = false;
                switch (pageName)
                {
                    case "Отчёты":
                        result = CurrentUser.IsPageReport;
                        break;
                    case "Адресная программа":
                        result = CurrentUser.IsPageAddress;
                        break;
                    case "Ассортимент":
                        result = CurrentUser.IsPageAssort;
                        break;
                    case "MML":
                        result = CurrentUser.IsPageMML;
                        break;
                    case "Продажи":
                        result = CurrentUser.IsPageSale;
                        break;
                    case "Пользователи":
                        result = CurrentUser.IsPageUser;
                        break;
                    case "Настройка":
                        result = CurrentUser.IsPageSetup;
                        break;
                    default:
                        result = false;
                        break;
                }
                return result;
            }
        }
    }
}